package com.video.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import com.video.bean.UserInfo;
import com.video.dao.UserInfoDao;
import com.video.service.UserServiceI;
import com.video.utils.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl implements UserServiceI {
	@Autowired
	private UserInfoDao userInfoDao;

	@Override
	public void register(UserInfo userInfo,HttpServletRequest req) {
		//1、上传文件
		MultipartFile headImage = userInfo.getHeadImage();

		String headImageName = headImage.getOriginalFilename(); //获取图片文件原来名称
		String headImageNewName = UUID.randomUUID().toString()+headImageName.substring(headImageName.lastIndexOf(".")); //生成图片新名称

		//数据库存储图片地址
		String headImageAddress = req.getServletContext().getContextPath()+"/images/"+headImageNewName;

		//图片真实上传地址：D:\IdeaProjects\VideoOnline\out\artifacts\VideoOnline_Web_exploded\
		String headImageRealAddress = req.getServletContext().getRealPath("/")+"images/"+headImageNewName;

		File targetCover = new File(headImageRealAddress);//图片目标文件
		try {
			headImage.transferTo(targetCover);//上传图片
		} catch (Exception e) {
			e.printStackTrace();
		}
		userInfo.setHeadImgAddress(headImageAddress);
		userInfoDao.register(userInfo);
	}

	@Override
	public ResultMap login(String username, String password, HttpServletRequest request) {
		HttpSession session = request.getSession();
		UserInfo user = userInfoDao.login(username,password);
		if(user != null){
			session.setAttribute("user",user);
			return ResultMap.createBySuccess(user);
		}else{
			return ResultMap.createByError("用户名或密码输入有误，请重新输入！");
		}
	}

	@Override
	public ResultMap selectAllUser(int page, int limit) {
		PageHelper.startPage(page, limit);//开启分页
		List<UserInfo> list = userInfoDao.selectAllUser();
		PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(list);
		list = pageInfo.getList();
		return ResultMap.selectLimitBySuccess(0,pageInfo.getTotal(),list);
	}

	@Override
	public ResultMap updateUserInfoByUsername(UserInfo userInfo,HttpServletRequest request) {
		HttpSession session = request.getSession();
		UserInfo user = (UserInfo)session.getAttribute("user");
		userInfo.setUsername(user.getUsername());
		session.invalidate();

		MultipartFile headImage = userInfo.getHeadImage();

		String headImageName = headImage.getOriginalFilename(); //获取图片文件原来名称
		String headImageNewName = UUID.randomUUID().toString()+headImageName.substring(headImageName.lastIndexOf(".")); //生成图片新名称

		//数据库存储图片地址
		String headImageAddress = request.getServletContext().getContextPath()+"/images/"+headImageNewName;

		//图片真实上传地址：D:\IdeaProjects\VideoOnline\out\artifacts\VideoOnline_Web_exploded\
		String headImageRealAddress = request.getServletContext().getRealPath("/")+"images/"+headImageNewName;

		File targetCover = new File(headImageRealAddress);//图片目标文件
		try {
			headImage.transferTo(targetCover);//上传图片
		} catch (Exception e) {
			e.printStackTrace();
		}
		userInfo.setHeadImgAddress(headImageAddress);
		userInfoDao.updateUserInfoByUsername(userInfo);
		return ResultMap.createBySuccess();
	}
}
